package logic

import (
	"context"
	"furni-set/funrpc/funrpc"
	"furni-set/funrpc/internal/svc"
	"furni-set/models/cache"
	"furni-set/models/mysql"
	"time"

	"github.com/zeromicro/go-zero/core/logx"
	"k8s.io/apimachinery/pkg/util/json"
)

type FruitListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewFruitListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FruitListLogic {
	return &FruitListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *FruitListLogic) FruitList(in *funrpc.FruitListRequest) (*funrpc.FruitListResponse, error) {
	// todo: add your logic here and delete this line
	var list []*funrpc.FruitList
	result, _ := cache.Rdb.Get(l.ctx, "list_").Result()
	if result == "" {
		list = mysql.List(mysql.DB, list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(l.ctx, "list_", jsonData, time.Minute*1)
	} else {
		json.Unmarshal([]byte(result), &list)
	}

	return &funrpc.FruitListResponse{
		List: list,
	}, nil
}
